package com.maslyk.invitation.service.email;

import javax.mail.BodyPart;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

import org.apache.log4j.Logger;

public class MIMEMultipartMixedService implements IEmailContentProcess 
{
	private String result = "";
	private Logger log = Logger.getLogger(MIMEMultipartMixedService.class);
	
	public String getContent(Object content) 
	{
		MimeMultipart multipart = (MimeMultipart)content;

		try
		 	{
				for(int i=0; i <= multipart.getCount(); i++)
				{
					BodyPart part = multipart.getBodyPart(i);
					
					String contentType = part.getContentType();
					
					if(contentType.contains("application"))
						continue;
					
					if(contentType.contains("multipart"))
						getContent(part.getContent());
					
					String cntnt = (String)part.getContent();
					
					
					if(cntnt.startsWith("=?"))
						result += MimeUtility.decodeWord(cntnt);
					else
						result += cntnt;
					
				}
			}
		 	catch (Exception e) 
			{
				log.error(e,e);
			} 
			
		return result;
	}
}
